/**
 * RMB市场专用收入/支出明细管理初始化
 */
var Market = {
    id: "MarketTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Market.initColumn = function () {
    return [
                [
                    {field: 'selectItem', radio: true,colspan: 1,rowspan: 2, width:'36px'},
                    {title: '日期', field: 'revdate', visible: true, align: 'center', valign: 'middle',colspan: 1,rowspan: 2,
                        formatter:function(value,row,index){
                            return value.substring(0,10);
                        }},
                    {title: '收入', field: 'income', visible: true,halign :"center", valign:"middle", align:"right",colspan: 1,rowspan: 2},
                    {title: '市场及推广费用支出', visible: true, align: 'center', valign: 'middle',colspan: 5,rowspan: 1},
                    {title: '余额', field: 'balance', visible: true, align: 'center', valign: 'middle',colspan: 1,rowspan: 2},
                    {title: '备注', field: 'remarks', visible: true, align: 'center', valign: 'middle',colspan: 1,rowspan: 2},
                ],
                [
                    {title: '微信抽奖', field: 'wcDraw', visible: true, halign :"center", valign:"middle", align:"right"},
                    {title: '群排名', field: 'groupRanking', visible: true, halign :"center", valign:"middle", align:"right"},
                    {title: 'QQ', field: 'qq', visible: true, halign :"center", valign:"middle", align:"right"},
                    {title: '其他推广',field: 'otherPromotion', visible: true, halign :"center", valign:"middle", align:"right"},
                    {title: '小计',field: 'payoutTatol', visible: true, halign :"center", valign:"middle", align:"right"}
                ]

    ];
};

/**
 * 检查是否选中
 */
Market.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Market.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加RMB市场专用收入/支出明细
 */
Market.openAddMarket = function () {
    var index = layer.open({
        type: 2,
        title: '添加RMB市场专用收入/支出明细',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/market/market_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看RMB市场专用收入/支出明细详情
 */
Market.openMarketDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: 'RMB市场专用收入/支出明细详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/market/market_update/' + Market.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 添加期初余额
 */
Market.openAddBalance = function () {
    var index = layer.open({
        type: 2,
        title: '添加期初余额',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/market/market_addBalance'
    });
    this.layerIndex = index;
};


/**
 * 删除RMB市场专用收入/支出明细
 */
Market.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/market/delete", function (data) {
            Feng.success("删除成功!");
            Market.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("marketId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 打开导出页面
 */
Market.operExport = function(){
    window.location.href = "/market/load?beginDate=" + $("#beginDate").val()+"&endDate=" + $("#endDate").val();
};


Market.resetSearch = function () {
    $("#platform").val("");
    $("#beginDate").val("");
    $("#endDate").val("");
}


Market.formParams = function(){
    var queryData = {};
    queryData['platform'] = $("#platform").val();
    queryData['beginDate'] = $("#beginDate").val();
    queryData['endDate'] = $("#endDate").val();
    return queryData;
}

/**
 * 查询RMB市场专用收入/支出明细列表
 */
Market.search = function () {

    Market.table.refresh({query: Market.formParams()});
    $("#MarketSum").remove();
    $("#MarketTable").after(  Market.tatol()   )
};

Market.tatol= function(){

    var tatol;
    var ajax = new $ax(Feng.ctxPath + "/market/total", function (result) {
        tatol=result;
    }, function (result) {
        Feng.error("查询失败!" + result.responseJSON.message + "!");
    });
    ajax.setData(Market.formParams());
    ajax.start();
    var rowTem = '<table id="MarketSum" data-mobile-responsive="true" data-click-to-select="true" class="table table-hover table-striped" style="margin-top: -0px;color: #2A2E36;font-size: 14px;"><tbody id="MarketSum" >'+
        '<tr data-index="0">'+
        '<td style="text-align: center; vertical-align: middle;width: 208px">总计：</td> '+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.incomeTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.wcTotal+'</td> '+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.groupTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.qqTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.otherTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle ">'+tatol.payoutTotal+'</td>'+
        '<td style="text-align: right; vertical-align: middle "></td>'+
        '<td style="text-align: right; vertical-align: middle "></td>'+
        '</tr></tbody></table>';
    return rowTem;
}



$(function () {
    var defaultColunms = Market.initColumn();
    var table = new BSTable(Market.id, "/market/list", defaultColunms,Market);
    table.setPaginationType("client");
    Market.table = table.init();
    $(".fixed-table-toolbar").after("<div style='text-align: center;vertical-align: middle;font-size: 22px;color: #6979d7;font-weight: bold;'> RMB市场专用收入/支出明细表</div>");
    $("#MarketTable").after(Market.tatol());
});
